<?php

class CartModel extends Model
{
	
	public function set($id, $data) {
		$id = intval($id);
	    if ($id) {
	        $this->update($data, 'id=' . $id);
	        return $id;
	    }
	    $this->insert($data);
	    if ($this->get_insert_id()) return $this->get_insert_id();
	    return false;
	}

    public function get_all_one($where){//根据购物车id查询
        $sql = "select * from ".$this->prefix."cart where $where";
        return $this->db->query($sql)->row_array();
    }

    public function get_one($id,$sessid){//根据购物车id查询
        $sql = "select * from ".$this->prefix."cart where shopid = $id and sessid = '".$sessid."'";
        return $this->db->query($sql)->row_array();
    }

    public function get_all_list($where,$order,$limit,$if_count=false){//查询购物车
        if($if_count){
            $sql = "select count(ca.id) as total from ".$this->prefix."cart ca "."
				 left join ".$this->prefix."content_1 s on ca.shopid=s.id "."
				 where $where";
            $res = $this->db->query($sql)->row_array();
            return $res['total'];
        }
        $sql = "select ca.id as cid,ca.*,s.*,sh.* from ".$this->prefix."cart ca "."
			 	 left join ".$this->prefix."content_1 s on ca.shopid=s.id 
			 	 left join ".$this->prefix."content_1_shop sh on ca.shopid=sh.id "."
				 where $where order by $order limit $limit";
        $res = $this->db->query($sql)->result_array();
        return $res;
    }
	
	public function get_all_heji($where,$order){
		  	 $sql = "select ca.* from ".$this->prefix."cart ca "."
			 	 left join ".$this->prefix."content_1 s on ca.shopid=s.id "."
				 where $where order by $order";
		return $this->db->query($sql)->result_array();
	}
	
	public function get_one_jiangli($id){
		$sql = "select jiangli from ".$this->prefix."cart where id=$id";
		$res = $this->db->query($sql)->row_array();
		return $res['jiangli'];
	}
	
	public function get_one_heji($id){
		$sql = "select price from ".$this->prefix."cart where id=$id";
		$res = $this->db->query($sql)->row_array();
		return $res['price'];
	}
	
	public function get_one_yunfei($id){
		$sql = "select yunfei from ".$this->prefix."cart where id=$id";
		$res = $this->db->query($sql)->row_array();
		return $res['yunfei'];
	}
	
	
	public function get_one_list($shopid,$sessid){//根据商品id与sessid查询
		$sql = "select id,number from ".$this->prefix."cart where shopid=$shopid and sessid='".$sessid."'";
		return $this->db->query($sql)->row_array();
	}
	
	//根据sessid查询购物车产品数量
	public function get_cart_num($sessid){
		$sql = "select sum(number) as number from ".$this->prefix."cart where sessid='".$sessid."'";
		$res = $this->db->query($sql)->row_array();
		return $res['number'];
	}

	public function dels($id=0){
        $sql = "delete from ".$this->prefix."cart where 1=1";
		if($id){
            $sql .=" and id=$id";
		}
		$this->db->query($sql);
	}
	
	
	public function get_yunfei_sess($sessid){
		if($sessid){
			$sql = "select max(yunfei) as yf from ".$this->prefix."cart where sessid='".$sessid."'";
			$res = $this->db->query($sql)->row_array();
			return $res['yf'];
		}
	}

			
	
}
?>